package cn.qiyu5522.service.impl;

import cn.qiyu5522.domain.DTO.TagAddDTO;
import cn.qiyu5522.domain.DTO.TagPageQueryDTO;
import cn.qiyu5522.domain.DTO.TagUpdateDTO;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.VO.TagVO;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.domain.entity.SystemException;
import cn.qiyu5522.domain.entity.Tag;
import cn.qiyu5522.enums.AppHttpCodeEnum;
import cn.qiyu5522.mapper.TagMapper;
import cn.qiyu5522.service.TagService;
import cn.qiyu5522.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 标签 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-16
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Override
    public ResponseResult getListByCondition(TagPageQueryDTO tagPageParamDTO) {
        // 标签名
        String name = tagPageParamDTO.getName();
        // 备注
        String remark = tagPageParamDTO.getRemark();
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                // 根据名称模糊查询
                .like(StringUtils.hasText(name), Tag::getName, name)
                .or()
                // 根据备注模糊查询
                .like(StringUtils.hasText(remark), Tag::getName, name);
        // 分页
        Page<Tag> page = new Page<>(tagPageParamDTO.getPageNum(), tagPageParamDTO.getPageSize());
        // 查询
        page(page,queryWrapper);
        // 获取数据集
        List<Tag> tags = page.getRecords();
        // 查询的标签总数
        long total = page.getTotal();
        // 转 VO
        List<TagVO> tagVOS = BeanCopyUtils.copyBeanList(tags, TagVO.class);
        PageVO pageVO = new PageVO(tagVOS,total);
        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult addTag(TagAddDTO tagAddDTO) {
        // 转实体类
        Tag tag = BeanCopyUtils.copyBean(tagAddDTO, Tag.class);
        boolean save = save(tag);
        if(!save){
            throw new SystemException(AppHttpCodeEnum.SAVE_OPERATE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTag(Long id) {
        boolean remove = removeById(id);
        if(!remove){
            throw new SystemException(AppHttpCodeEnum.DELETE_OPERATE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTag(Long id) {
        Tag tag = getById(id);
        // 转VO
        TagVO tagVO = BeanCopyUtils.copyBean(tag, TagVO.class);
        return ResponseResult.okResult(tagVO);
    }

    @Override
    public ResponseResult updateTag(TagUpdateDTO tagUpdateDTO) {
        // 转 实体类
        Tag tag = BeanCopyUtils.copyBean(tagUpdateDTO, Tag.class);
        boolean update = updateById(tag);
        if (!update) {
            throw new SystemException(AppHttpCodeEnum.UPDATE_OPERATE_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllTag() {
        List<Tag> list = list();
        List<TagVO> tagVOS = BeanCopyUtils.copyBeanList(list, TagVO.class);
        return ResponseResult.okResult(tagVOS);
    }
}
